// pages/shop/shop.js
let shopCtrl = require('../../utils/shopCtrl')
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    selectAll: true,
    total: 0,
    price: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCartList();
    // let data = [{
    //   gid : '78876',
    //   gtitle : '商品标题',
    //   gprice : '99',
    //   gnum : '2',
    //   gimgurl : '商品缩略图',
    //   gchecked : true
    // }];
    // shopCtrl.setCartData(data);
    // let arr = shopCtrl.getCartData();
    // console.log(arr)
  },
  onShow(){
    //不会缓存
    //获取数据渲染页面
    this.getCartList();
  },
  getCartList() {
    let goodsList = shopCtrl.getCartData();
    this.setData({goodsList});
    let {total, price} = shopCtrl.getPriceCount();
    this.setData({total, price})
  },
  selOne(ev){
    let {gid} = ev.target.dataset;
    console.log('gid是', gid);
    let selectAll = shopCtrl.selOneGood(gid);
     //获取数据渲染页面
     this.getCartList();
     //单选控制全选
     this.setData({selectAll});
  },
  selAllGood(){
    let status = !this.data.selectAll;
    shopCtrl.selAll(status);
    this.getCartList();
    this.setData({selectAll:status});
  },
  addNum(ev){
    let {gid} = ev.target.dataset;
    shopCtrl.addGoodNum(gid);
    //获取数据渲染页面
    this.getCartList();
  },
  cutNum(ev){
    let {gid} = ev.target.dataset;
    shopCtrl.cutGoodNum(gid);
    //获取数据渲染页面
    this.getCartList();
  },
  onClose(ev){
    const { instance } = ev.detail;
    let {gid} = ev.target.dataset;
    Dialog.confirm({
      title: '警告',
      message: '您确定要删除我吗？',
    })
      .then(() => {
        // on confirm
        console.log('确定了');
        //删除某个商品
        shopCtrl.delect(gid);
        //获取数据渲染页面
        this.getCartList();
        instance.close();//关闭滑块
      })
      .catch(() => {
        // on cancel
        console.log('取消了');
        instance.close();
      });
  },
  onSubmit() {
    console.log('准备跳转订单页');
    wx.navigateTo({
      url: '/pages/order/order',
    })
  }
})